// Package request rpc请求处理
package request

import (
	"fmt"
	"net/rpc"
	"gitee.com/lzqysoft/framework/common"
	"gitee.com/lzqysoft/framework/common/log"
	"os"
)

const (
	BASE = 1 << iota

)

var (
	base  *rpc.Client
)

func init()  {
	log.Info("开始建立rpc连接")
	if client1, err := rpc.DialHTTP("tcp", "localhost:8001"); err != nil {
		log.Error(err)
		log.Error("base 模块rpc连接建立失败，退出程序")
		os.Exit(1)
	} else {
		log.Info("base 模块rpc连接建立成功")
		base = client1
	}
}

func Request(module,service,token  string, arg map[string]interface{}) *common.Result {
	client := chooseClient(module)
	var args = &common.Arg{token, arg}
	var reply = &common.Result{}
	err := client.Call(service, args, reply)
	if err != nil {
		fmt.Println("arith error:", err)
		reply.Res = nil
		reply.Code = "0003"
		reply.Message = "没有此服务"
		return reply
	}
	//log.Info(reply)
	return reply
}

// chooseClient 根据服务名返回对应的rpc客户端
func chooseClient(module string) *rpc.Client{
	if module == "BASE" {
		return base
	}
	return base
}